package com.ssm.studen.service.impl;

import com.ssm.studen.dao.AnswerDao;
import com.ssm.studen.dao.StudentDao;
import com.ssm.studen.model.Answer;
import com.ssm.studen.model.Student;
import com.ssm.studen.service.AnswerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 答题表：
 *      实现业务及流程接口当中的内容
 */
@Service
public class AnswerServiceImpl implements AnswerService {

    @Autowired
    private AnswerDao answerDao;
    @Autowired
    private StudentDao studentDao;

    /**
     * 答题表的添加
     * @param answer
     * @return
     */
    @Override
    public int addAnswer(Answer answer) {
        int returnInt = 0;
        /*
         * 1.业务判断
         *          1）角色名称不能重复
         * */
        //1）通过判断答题人的学号来判断试卷是否重复
        int studentIdCount = studentDao.getStudentIdCount(new Student().getSid());
        if(studentIdCount<1){
            //2.数据添加
            int addAnswer = answerDao.insertAnswer(answer);
            //3.返回值设置
            returnInt = addAnswer;
        }else{
            //有相同的学号
            returnInt = -1;
        }
        return returnInt;
    }


    /**
     * 答题表的更新
     * @param answer
     * @return
     */
    @Override
    public int updateAnswer(Answer answer) {
        /*
         * 业务流程判断
         * */
        int isUpdate = 0;
        try {
            isUpdate = answerDao.updateAnswer(answer);
        }catch (Exception e){
            isUpdate = -1;
            //打印更新时报错的情况
            System.out.println("更新试卷信息时执行SQL报错的原因是：" + e.getMessage());
        }
        return isUpdate;
    }


    /**
     * 答题表的查询
     * @param anum
     * @return
     */
    @Override
    public List<Answer> selectAnswer(int anum) {
        List<Answer> answers = answerDao.selectAnswer(anum);
        return answers;
    }
}
